package com.szxy.controller.system;

import com.szxy.aspectj.annotation.Log;
import com.szxy.aspectj.enums.BusinessType;
import com.szxy.aspectj.enums.OperatorType;
import com.szxy.dto.DictTypeDto;
import com.szxy.service.DictTypeService;
import com.szxy.utils.ShiroSecurityUtils;
import com.szxy.vo.AjaxResult;
import com.szxy.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @Author: zwz
 * @Date:2020/11/5 22:31
 * @Description:
 **/
@RestController
@RequestMapping("system/dict/type")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /*** 分页查询 */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DictTypeDto dictTypeDto) {
        DataGridView gridView = this.dictTypeService.listPage(dictTypeDto);
        return AjaxResult.success("查询成功", gridView.getData(), gridView.getTotal());
    }

    /*** 添加 */
    @PostMapping("addDictType")
    @Log(title = "添加字典类型", businessType = BusinessType.INSERT, operatorType = OperatorType.OTHER)
    public AjaxResult addDictType(@Validated DictTypeDto dictTypeDto) {
        if (dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType())) {
            return AjaxResult.fail("新增字典【" + dictTypeDto.getDictName() + "】失败，字典类型已存在");
        }
        dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.insert(dictTypeDto));
    }

    /*** 修改 */
    @PutMapping("updateDictType")
    @Log(title = "更新字典数据", businessType = BusinessType.UPDATE, operatorType = OperatorType.OTHER)
    public AjaxResult updateDictType(@Validated DictTypeDto dictTypeDto) {
        if (dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType())) {
            return AjaxResult.fail("修改字典【" + dictTypeDto.getDictName() + "】失败，字典类型已存在");
        }
        dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.update(dictTypeDto));
    }

    /*** 根据 ID 查询一个字典信息 */
    @GetMapping("getOne/{dictId}")
    public AjaxResult getDictType(@PathVariable @Validated @NotNull(message = "字典ID不能为空") Long dictId) {
        return AjaxResult.success(this.dictTypeService.selectDictTypeById(dictId));
    }

    /*** 删除 */
    @DeleteMapping("deleteDictTypeByIds/{dictIds}")
    @Log(title = "删除字典数据", businessType = BusinessType.DELETE, operatorType = OperatorType.OTHER)
    public AjaxResult updateDictType(@PathVariable @Validated @NotEmpty(message = "要删除的 ID 不 能为空") Long[] dictIds) {
        return AjaxResult.toAjax(this.dictTypeService.deleteDictTypeByIds(dictIds));
    }

    /*** 查询所有可用的字典类型 */
    @GetMapping("selectAllDictType")
    public AjaxResult selectAllDictType() {
        return AjaxResult.success(this.dictTypeService.list().getData());
    }

    @GetMapping("dictCacheAsync")
    public AjaxResult dictCacheSync() {
        try{
            this.dictTypeService.dictCacheSync();
            return AjaxResult.success("缓存成功");
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
